package com.yogee.topseller.fragment;

import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.lcodecore.tkrefreshlayout.TwinklingRefreshLayout;
import com.yogee.core.base.HttpFragment;
import com.yogee.core.base.HttpFragment1;
import com.yogee.core.http.listener.HttpOnNextListener;
import com.yogee.core.http.subscribers.BaseSubscriber;
import com.yogee.core.utils.AnimalUtil;
import com.yogee.core.utils.ToastUtils;
import com.yogee.topseller.R;
import com.yogee.topseller.activity.ZhoubianActivity;
import com.yogee.topseller.activity.login.view.impl.RegisterLabelActivity;
import com.yogee.topseller.adapter.IntelligentScreeningBigRvAdapter;
import com.yogee.topseller.adapter.IntelligentScreeningListRvAdapter;
import com.yogee.topseller.bean.GongsiProBean;
import com.yogee.topseller.bean.InterlligentFilterBean;
import com.yogee.topseller.bean.JigouBean;
import com.yogee.topseller.http.HttpManager;
import com.yogee.topseller.interfaces.MyListener;
import com.yogee.topseller.utils.AppUtil;
import com.yogee.topseller.utils.LogUtils;
import com.yogee.topseller.utils.RefreshUtil;
import com.yogee.topseller.view.BasicDialog;
import com.yogee.topseller.view.EmptyRecyclerView;
import com.yogee.topseller.view.MySinaRefreshView;
import com.yogee.topseller.view.RefreshView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * 公司相关
 *
 * @auathor ${XiaoJianghong}
 * @date 2017/6/26.
 */
public class GongsiProFragment extends HttpFragment1 implements ZhoubianActivity.UpdateListener,RefreshUtil.OnRefreshListener{
    @BindView(R.id.big_img_tv)
    TextView bigImgTv;
    @BindView(R.id.list_tv)
    TextView listTv;
    @BindView(R.id.intelligent_screening_rv)
    EmptyRecyclerView rv;
    @BindView(R.id.refreshLayout)
    TwinklingRefreshLayout refreshLayout;
    @BindView(R.id.id_empty_view)
    View id_empty_view;
    @BindView(R.id.iv_empty)
    ImageView iv_empty;
    @BindView(R.id.tv_miaoshu)
    TextView tv_miaoshu;
    Unbinder unbinder;


    private IntelligentScreeningListRvAdapter listAdapter;//列表适配器

    private List<InterlligentFilterBean.ProductListBean> bean = new ArrayList<>();
    private boolean isBigImg = false;
    private boolean isLogin;
    private int isMj = 0;
    JigouBean.Company companys;
    @Override
    protected int getLayoutId() {
        return R.layout.fragment_gs_product;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {


        return super.onCreateView(inflater, container, savedInstanceState);
    }

    @Override
    protected void initView() {
        ((ZhoubianActivity)getActivity()).setListener3(this);
        MySinaRefreshView headerView = new MySinaRefreshView(getActivity());
        refreshLayout.setOnRefreshListener(new RefreshUtil(this).refreshListenerAdapter());
        refreshLayout.setHeaderView(headerView);
        refreshLayout.setBottomView(new RefreshView(getActivity()));
        refreshLayout.setBottomHeight(50);

        iv_empty.setImageResource(R.drawable.nodata3);
        tv_miaoshu.setText("暂无相关产品信息");

        listAdapter = new IntelligentScreeningListRvAdapter(getActivity());
        rv.setLayoutManager(new LinearLayoutManager(getActivity()));
        rv.setHasFixedSize(true);
        rv.setAdapter(listAdapter);
        rv.setEmptyView(id_empty_view);

    }

    private void getPro(final String refrush, JigouBean.Company company, String total, String count){
        HttpManager.getInstance().relatedCompanyTypeProductList(company.getId(),total,count).compose(this.bindRecycler()).subscribe(new BaseSubscriber(new HttpOnNextListener<GongsiProBean>() {
            @Override
            public void onNext(GongsiProBean o) {
//                list1.addAll(o.getMechanismList());
                // 刷新
                if(refrush.equals("1")){
                    refreshLayout.finishRefreshing();
                    bean = o.getRelatedCompanyTypeProductList();
                }else{
                    refreshLayout.finishLoadmore();
                    bean.addAll(o.getRelatedCompanyTypeProductList());
                }
                listAdapter.addData(bean);

            }
        }, this));
    }

    @Override
    public void onResume() {
        super.onResume();
        companys = ((ZhoubianActivity)getActivity()).getMySelectCompany();
        if(companys!=null)
            getPro("1",companys,"0","10");

    }


    @Override
    public void showMsg(String msg) {
        super.showMsg(msg);

    }

    @Override
    public void getCurrentPos(JigouBean.Company company) {
        companys = company;
        if(company!=null){

            LogUtils.e("sfz","产品:"+company.getName());
            getPro("1",company,"0","10");
        }else{
            listAdapter.addData(null);
        }

    }

    @Override
    public void onRefresh() {
        if(companys!=null)
            getPro("1",companys,"0","10");
        else{
            refreshLayout.finishRefreshing();
        }
    }

    @Override
    public void onLoad() {
        if(companys!=null)
            getPro("0",companys,String.valueOf(bean.size()),"10");
    }
}